/** Inventory mode handler.
 *@param buffer The buffer to render to.
 *@param mode The mode to start on (optional).
 */
function InventoryMode(buffer, mode)
{
	this.buffer = buffer;
	this.mode = mode ? mode : "use";
	this.width = 69;
	this.height = 20;
	this.x = Math.floor((this.buffer.width - this.width) / 2);
	this.y = Math.floor((this.buffer.height - this.height) / 2);
	this.closed = false;
	this.page = 0;
}
InventoryMode.prototype =
{
	/** Data for mode selection line rendering.
	 */
	modes: ["use", "drop", "equip", "throw"],
	modeNames: ["   a)Activate", "     d)Drop", "     e)Equip", "     t)Throw    "],
	modeLetters: ["b", "c", "f", "g", "i", "m", "n", "o", "p", "q", "r", "s", "u", "v", "w", "x"],
	/** Close this mode handler.
	 */
	close: function()
	{
		dhack.removeModeHandler(this);
		this.closed = true;
	},
	/** Handle an input action.
	 *@param action The action string to handle.
	 *@param chr The character string that prompted the action (optional).
	 */
	handleAction: function(action, chr)
	{
		if(!chr ||
			this.modeLetters.indexOf(chr) < 0)
		{
			switch(action)
			{
				case "cancel":
					this.close();
					break;
				case "inventory": this.switchMode("use"); break;
				case "drop": this.switchMode("drop"); break;
				case "equip": this.switchMode("equip"); break;
				case "throw": this.switchMode("throw"); break;
				default:
					break;
			}
		}
		else
		{
			var selection = this.modeLetters.indexOf(chr);
			if(selection < 0)
				return true;
			selection += this.page * 16;
			if(selection >= this.items.length)
				return true;
			var item = this.items[selection];
			switch(this.mode)
			{
				case "drop":
					dhack.player.dropItem(item);
					break;
				case "equip":
					if(dhack.player.isEquiped(item))
						dhack.player.unequipItem(item);
					else
						dhack.player.equipItem(item);
					break;
			}
			this._renderStats();
			this._renderMode();
		}
		return true;
	},
	/** Switch to a different mode and update the display.
	 *@param mode The mode string to switch to.
	 */
	switchMode: function(mode)
	{
		this.mode = mode;
		this._renderModeLine();
		this._renderMode();
	},
	/** Rerender the entire display.
Player Name.....| a)Activate d)Drop e)Equip t)Throw
Health      100%+----------------------------------------------------
Stamina     100 | b) E Item Name.......................... 000.0 00.0
Strength     20 | c) E Item Name.......................... 000.0 00.0
Agility      20 | f) E Item Name.......................... 000.0 00.0
Endurance    20 | g) E Item Name.......................... 000.0 00.0
Damage      100 | i) E Item Name.......................... 000.0 00.0
Protection  100%| m) E Item Name.......................... 000.0 00.0
Weight      100%| n) E Item Name.......................... 000.0 00.0
----------------+ o) E Item Name.......................... 000.0 00.0
     Skills     | p) E Item Name.......................... 000.0 00.0
Athletics   100%| q) E Item Name.......................... 000.0 00.0
Boxing      100%| r) E Item Name.......................... 000.0 00.0
Defense     100%| s) E Item Name.......................... 000.0 00.0
Fighting    100%| v) E Item Name.......................... 000.0 00.0
Judo        100%| x) E Item Name.......................... 000.0 00.0
Kung-Fu     100%| y) E Item Name.......................... 000.0 00.0
Making      100%| z) E Item Name.......................... 000.0 00.0
Wrestling   100%+----------------------------------------------------
Throwing    100%|< Left                Page 1                 Arrow >
	 */
	render: function()
	{
		if(this.closed)
			return;
		
		// Menu frame
		this.buffer.drawMenuBox(
			this.x,
			this.y,
			this.width,
			this.height,
			Color.white,
			Color.darkBlue
		);
		
		// Dividers and such
		this.buffer.blitRect(this.x + 16, this.y, 1, this.height, Char.DBAR_NS, Color.white, Color.darkBlue);
		this.buffer.blitRect(this.x + 17, this.y + 1, this.width - 17, 1, Char.DBAR_EW, Color.white, Color.darkBlue);
		this.buffer.blitRect(this.x + 17, this.y + 18, this.width - 17, 1, Char.DBAR_EW, Color.white, Color.darkBlue);
		this.buffer.blitRect(this.x, this.y + 9, 16, 1, Char.DBAR_EW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + 16, this.y + 1, Char.DBAR_NES, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + 16, this.y + 9, Char.DBAR_NSW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + 16, this.y + 18, Char.DBAR_NES, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + 16, this.y - 1, Char.DBAR_ESW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + 16, this.y + this.height, Char.DBAR_NEW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + this.width, this.y + 1, Char.DBAR_NSW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x + this.width, this.y + 18, Char.DBAR_NSW, Color.white, Color.darkBlue);
		this.buffer.setCell(this.x - 1, this.y + 9, Char.DBAR_NES, Color.white, Color.darkBlue);
		
		// Render sections
		this._renderStats();
		this._renderModeLine();
		this._renderMode();
	},
	/** Render the player's attributes and stats.
	 */
	_renderStats: function()
	{
		var weightP = dhack.player.weight / dhack.player.weightMax;
		weightP *= 100;

		this.buffer.blitStringCentered(this.x, this.y, "%f4" + dhack.player.name, 16, Color.darkBlue);
		this.buffer.blitString(this.x, this.y + 1, "%f4Health      " + util.leftPad(util.floatDigits(dhack.player.hp, 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 2, "%f4Stamina     " + util.leftPad(util.floatDigits(dhack.player.sp, 0), 3) + " ");
		this.buffer.blitString(this.x, this.y + 3, "%f4Strength    " + util.leftPad(util.floatDigits(dhack.player.str, 0), 3) + " ");
		this.buffer.blitString(this.x, this.y + 4, "%f4Agility     " + util.leftPad(util.floatDigits(dhack.player.agi, 0), 3) + " ");
		this.buffer.blitString(this.x, this.y + 5, "%f4Endurance   " + util.leftPad(util.floatDigits(dhack.player.end, 0), 3) + " ");
		this.buffer.blitString(this.x, this.y + 6, "%f4Damage      " + util.leftPad(util.floatDigits(dhack.player.getMeleeDamage(), 0), 3) + " ");
		this.buffer.blitString(this.x, this.y + 7, "%f4Protection  " + util.leftPad(util.floatDigits(dhack.player.getProtectionPercent(), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 8, "%f4Weight      " + util.leftPad(util.floatDigits(weightP, 0), 3) + "%%");
		this.buffer.blitStringCentered(this.x, this.y + 10, "%f4Skills", 16, Color.darkBlue);
		this.buffer.blitString(this.x, this.y + 11, "%f4Athletics   " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("athletics"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 12, "%f4Boxing      " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("boxing"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 13, "%f4Defense     " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("defense"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 14, "%f4Fighting    " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("fighting"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 15, "%f4Judo        " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("judo"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 16, "%f4Kung-Fu     " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("kungfu"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 17, "%f4Making      " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("making"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 18, "%f4Wrestling   " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("wrestling"), 0), 3) + "%%");
		this.buffer.blitString(this.x, this.y + 19, "%f4Throwing    " + util.leftPad(util.floatDigits(dhack.player.getSkillValue("throwing"), 0), 3) + "%%");
	},
	/** Render the mode selection line.
	 */
	_renderModeLine: function()
	{
		var i, col, ofs = 17;
		for(var i = 0; i < this.modes.length; ++i)
		{
			if(this.modes[i] == this.mode)
				col = "%f4";
			else
				col = "%74";
			this.buffer.blitString(this.x + ofs, this.y + 0, col + this.modeNames[i], this.modeNames[i].length);
			ofs += this.modeNames[i].length;
		}
	},
	/** Render the mode.
	 */
	_renderMode: function()
	{
		// Page selection
		this.items = dhack.player.inventory;
		this.pages = (dhack.player.inventory.length / this.modeLetters.length) | 0;
		if(this.pages * this.modeLetters.length < dhack.player.inventory.length)
			++this.pages;
		if(this.page >= this.pages)
			this.page = this.pages - 1;
		if(this.page < 0)
			this.page = 0;
		
		// Pagiation line
		var leftColor = "%f4";
		if(this.page == 0)
		{
			this.buffer.setCell(this.x + 17, this.y + 19, Char.POINTER_LEFT, Color.gray, Color.darkBlue);
			leftColor = "%74";
		}
		else
			this.buffer.setCell(this.x + 17, this.y + 19, Char.POINTER_LEFT, Color.white, Color.darkBlue);
		var rightColor = "%f4";
		if(this.page >= this.pages - 1)
		{
			this.buffer.setCell(this.x + this.width - 1, this.y + 19, Char.POINTER_RIGHT, Color.gray, Color.darkBlue);
			rightColor = "%74";
		}
		else
			this.buffer.setCell(this.x + this.width - 1, this.y + 19, Char.POINTER_RIGHT, Color.white, Color.darkBlue);
		this.buffer.blitString(this.x + 18, this.y + 19, leftColor + " Left                %f4Page " + (this.page + 1) + "                 " + rightColor + "Right ");
		
		// No items to list
		if(this.items.length == 0)
		{
			this.buffer.blitRect(this.x + 17, this.y + 2, this.width - 17, this.height - 4, " ", Color.white, Color.darkBlue);
			this.buffer.blitStringCentered(this.x + 17, this.y + ((this.height / 2) | 0), "%74--- Nothing ---", this.width - 17, Color.darkBlue);
			return;
		}
		
		// Item list, yay!
		for(var i = 0; i < 16; ++i)
		{
			var itemIdx = this.page * this.modeLetters.length + i;
			if(itemIdx >= this.items.length)
			{
				this.buffer.blitRect(this.x + 17, this.y + 2 + i, this.width - 17, 1, " ", Color.white, Color.darkBlue)
			}
			else
			{
				var item = this.items[itemIdx];
				var equipFlag = " ";
				if(dhack.player.isEquiped(item))
				{
					equipFlag = item.getEquipFlag();
					if(item === dhack.player.offHandWeapon)
						equipFlag = "w";
				}
				this.buffer.bg(Color.darkBlue);
				
				// Determine if we can take action on this item in this mode
				var isActionable = false;
				switch(this.mode)
				{
					case "use":
						if(item.canUse())
							isActionable = true;
						break;
					case "throw":
					case "drop":
						isActionable = true;
						break;
					case "equip":
						if(item.isEquipable())
							isActionable = true;
						break;
				}
				
				// Determine what colors to use for highlighting
				var unactionableColor = Color.gray;
				var actionableColor = Color.white;
				if(dhack.player.equipPenalty(item))
				{
					unactionableColor = Color.brown;
					actionableColor = Color.yellow;
				}
				if(isActionable)
					this.buffer.fg(actionableColor);
				else
					this.buffer.fg(unactionableColor);
				this.buffer.blitString(
					this.x + 17,
					this.y + 2 + i,
					" " + this.modeLetters[i] + ") " + equipFlag + " " +
					util.padOrTruncate(item.name, this.width - 34) +
					" " + util.leftPad(util.floatDigits(item.weight, 1), 5) +
					" " + util.leftPad(util.floatDigits(item.mods.ac, 1), 4)
				);
			}
		}
	}
}
